/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MatrixCalculation;

import java.text.DecimalFormat;

/**
 *
 * @author Ekantik
 */
public class Trust {
    
    private double trust[][];
    private double degreeOfTrust[][];
    RatingMatrix rm;
    public double[][] getDegreeOfTrust() {
        return degreeOfTrust;
    }

    public void setDegreeOfTrust(double[][] degreeOfTrust) {
        this.degreeOfTrust = degreeOfTrust;
    }
    
    public double[][] getTrust() {
        return trust;
    }

    public void setTrust(double[][] trust) {
        this.trust = trust;
    }
    Trust(){
        NormalizedMatrix nm = new NormalizedMatrix();
        RatingMatrix rm = new RatingMatrix();
        this.rm = rm;
        SimilarityMatrix sm = new SimilarityMatrix(this.rm);
        System.out.println("This is Rating Matrix:-");
        this.print(rm.getRatMatrix());
        System.out.println("This is Similarity Matrix-");
        this.print(sm.getSimilarityMatrix());
        System.out.println("This is Normalized Matrix:-");
        this.print(nm.getNormalizedMatrix());
        System.out.println("This is degree of Trust:-");
        this.print(nm.getdOTrust());
        this.trust = new double[rm.getNoOfUsers()][rm.getNoOfServices()];
        
        this.setDegreeOfTrust(nm.getdOTrust());
        double sum =0;
        for(int u = 0;u<rm.getNoOfUsers();u++){
            for(int i = 0;i<rm.getNoOfUsers();i++){
                sum = 0;
                for(int v=0;v<rm.getNoOfServices();v++){
                    if(u != v){
                        sum += this.getDegreeOfTrust()[v][i];
                    }
                }
                this.trust[u][i] = this.RoundTo2Decimals(sum/(rm.getNoOfUsers()-1));
            }
        }
        System.out.println("This is Trust Matrix:-");
        this.print(trust);
        System.out.println("Printing Reliability Matrix:-");
        this.calculateReliability();
        this.print(this.getRel(),this.index);
        this.mostReliableService();
    }

    public void print(int[][] matrix){
        for(int i = 0;i<rm.getNoOfUsers();i++){
            for(int j=0;j<rm.getNoOfServices();j++){
                System.out.print(this.RoundTo2Decimals(matrix[i][j])+"\t");
            }
            System.out.println();
        }
    }
    
    double RoundTo2Decimals(double val) {
            DecimalFormat df2 = new DecimalFormat("###.##");
        return Double.valueOf(df2.format(val));
    }
        
    
    public void print(double[][] matrix){
        for(int i = 0;i<rm.getNoOfUsers();i++){
            for(int j=0;j<rm.getNoOfServices();j++){
                System.out.print(matrix[i][j]+"\t");
            }
            System.out.println();
        }
    }
    public void print(double[] matrix,int[] index){
        for(int i = 0;i<matrix.length-1;i++){
            System.out.println("["+i+"]"+matrix[i] + "ind:"+index[i]);
        }
    }
    public void print(double[] matrix){
        for(int i = 0;i<matrix.length;i++){
            System.out.println("["+i+"]"+matrix[i]);
        }
    }
    double rel[];

    public double[] getRel() {
        return rel;
    }

    public void setRel(double[] rel) {
        this.rel = rel;
    }
    int index[];
    
    public void calculateReliability(){
        this.rel = new double[rm.getNoOfUsers()];
        this.index = new int[rm.getNoOfUsers()];
        for(int u = 0;u<rm.getNoOfUsers();u++){
            for(int i = 0;i<rm.getNoOfServices();i++){
                if(rel[u]< this.getTrust()[u][i]){
                    this.rel[u] = this.RoundTo2Decimals(this.getTrust()[u][i]);
                    //System.out.println(rel[u]);
                    index[u]=i;
                }
            }
        }
        //this.print(rel,index);
    }
    public void mostReliableService(){
        double array[] = new double[rm.getNoOfUsers()];
        double max;
        int service = 0;
        for(int i =0;i<rm.getNoOfUsers();i++){
            array[index[i]] +=1;
        }
        max = array[0];
        for(int i=0;i<rm.getNoOfUsers();i++){
            if(max<array[i]){
                max= array[i];
                service = i;
            }
        }
        /*System.out.print("Printing Sum of Arrays:-");
        for(int i=0;i<rm.getNoOfUsers();i++){
            System.out.println("["+i+"]"+array[i]);
        }*/
        System.out.println("Printing Most Reliable Service:" + service);
        
    }
    public static void main(String args[]){
        Trust t = new Trust();
        
        NormalizedMatrix nm = new NormalizedMatrix();
        RatingMatrix rm = new RatingMatrix();
        t.rm = rm;
        t.trust = new double[rm.getNoOfUsers()][rm.getNoOfServices()];
        t.setDegreeOfTrust(nm.getdOTrust());
        double sum =0;
        for(int u = 0;u<rm.getNoOfUsers();u++){
            for(int i = 0;i<rm.getNoOfUsers();i++){
                sum = 0;
                for(int v=0;v<rm.getNoOfServices();v++){
                    if(u != v){
                        sum += t.getDegreeOfTrust()[v][i];
                    }
                }
                t.trust[u][i] = sum/(rm.getNoOfUsers()-1);
                //System.out.print("["+u+"]["+i+"]"+(sum/9)+"\t");
            }
            //System.out.println();
        }
        System.out.println("Printing Reliability Matrix:-");
        t.calculateReliability();
        t.mostReliableService();
        
        
    }
}
